/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.core.service.impl;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.mapper.BeanMapper;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.core.entity.Email;
import com.intelligent.ispc.core.repository.jpa.EmailDao;
import com.intelligent.ispc.core.service.EmailService;
import com.intelligent.ispc.foundation.mail.MailConfiguration;
import com.intelligent.ispc.foundation.mail.EmailStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Julia.Zhou on 2015/12/3.
 */
@Service
class EmailServiceImpl implements EmailService {
    private static Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);

    @Autowired
    private EmailDao emailDao;

    @Override
    public void saveEmail(MailConfiguration emailDto) {
        if (emailDto == null || emailDto.getName() == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_PARAMETER_INPUT_INVALID);
        }

        Email email = new Email();
        BeanMapper.copy(emailDto, email);
        List<Email> emails = emailDao.findByStatus(EmailStatus.ACTIVE);
        if(emails != null && !emails.isEmpty()) {
            email.setStatus(EmailStatus.INACTIVE);
        }else
            email.setStatus(EmailStatus.ACTIVE);
        email.setCreateTime(new Date());

        logger.debug("Saving email:{}", emailDto);
        emailDao.save(email);
        logger.info("Save done, email={}", email.getName());
    }

    @Override
    public void updateEmail(MailConfiguration emailDto) {
        if (emailDto == null || emailDto.getId() == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_PARAMETER_INPUT_INVALID);
        }
        List<Email> emailList = emailDao.findByStatus(EmailStatus.ACTIVE);
        if(emailList != null && !emailList.isEmpty()) {
            if(EmailStatus.ACTIVE.equals(emailDto.getStatus())) {
                emailList.get(0).setStatus(EmailStatus.INACTIVE);
                emailDao.save( emailList.get(0));
            }
        }
        Email email = new Email();
        BeanMapper.copy(emailDto, email);
        email.setUpdateTime(new Date());
        logger.debug("Updating email:{}", emailDto);
        emailDao.save(email);
        logger.info("Update done, email={}", email.getName());
    }

    @Override
    public void updateEmailStatus(String emailId, EmailStatus status) {
        if (status == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_PARAMETER_INPUT_INVALID);
        }

        List<Email> emailList = emailDao.findByStatus(EmailStatus.ACTIVE);
        if(emailList != null && !emailList.isEmpty()) {
            if(EmailStatus.ACTIVE.equals(status)) {
                emailList.get(0).setStatus(EmailStatus.INACTIVE);
                emailDao.save( emailList.get(0));
            }
        }

        Email email = emailDao.findById(Long.valueOf(emailId));
        if (email == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_OBJECT_DOES_NOT_EXIST);
        }
        email.setStatus(status);
        emailDao.save(email);
        logger.info("Update done, email={}", email);
    }

    @Override
    public void deleteEmail(String uuid) {
        if (uuid == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_PARAMETER_INPUT_INVALID);
        }
        Email email = emailDao.findById(Long.valueOf(uuid));
        if (email == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_OBJECT_DOES_NOT_EXIST);
        }

        logger.debug("Deleting email:{}", email.getName());
        emailDao.delete(email);
        logger.info("Delete done, email={}", email.getName());
    }

    @Override
    public MailConfiguration findByUuid(String uuid) {
        if (uuid == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_PARAMETER_INPUT_INVALID);
        }

        Email email = emailDao.findById(Long.valueOf(uuid));

        if (email == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_OBJECT_DOES_NOT_EXIST);
        }

        MailConfiguration emailDto = new MailConfiguration();
        BeanMapper.copy(email, emailDto);

        return emailDto;
    }

    @Override
    public List<MailConfiguration> findByStatus(EmailStatus status) {
        List<Email> emails = emailDao.findByStatus(status);
        List<MailConfiguration> emailDtos = Lists.newArrayList();
        if(emails != null && !emails.isEmpty()) {
            for (Email email : emails) {
                MailConfiguration emailDto = new MailConfiguration();
                BeanMapper.copy(email, emailDto);
                emailDtos.add(emailDto);
            }
        }
        return emailDtos;
    }

    @Override
    public List<MailConfiguration> findAllEmails() {
        List<Email> emails = (List<Email>)emailDao.findAll();
        if (emails == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_OBJECT_DOES_NOT_EXIST);
        }

        List<MailConfiguration> emailDtos = Lists.newArrayList();
        for(Email email : emails){
            MailConfiguration emailDto = new MailConfiguration();
            BeanMapper.copy(email,emailDto);
            emailDtos.add(emailDto);
        }

        return emailDtos;
    }

}
